Hands-on Ex1: Geospatial Data Wrangling with R

Overview

In this hands-on exercise, i learn how to import and wrangle geospatial data in using appropriate R packages.

Getting Started

  • sf for importing, managing, and processing geospatial data, and

  • tidyverse for performing data science tasks such as importing, wrangling and visualising data.

Tidyverse consists of a family of R packages. In this hands-on exercise, the following packages will be used:

  • readr for importing csv data,

  • readxl for importing Excel worksheet,

  • tidyr for manipulating data,

  • dplyr for transforming data, and

  • ggplot2 for visualising data

The code chunk below install and load sf and tidyverse packages into R environment.

pacman::p_load(sf, tidyverse)

Importing Geospatial Data

how to import the following geospatial data into R by using st_read() of sf package:

  • MP14_SUBZONE_WEB_PL, a polygon feature layer in ESRI shapefile format,

  • CyclingPath, a line feature layer in ESRI shapefile format, and

  • PreSchool, a point feature layer in kml file format.

Importing Polygon feature data in shapefile form

below uses st_read() function of sf package to import MP14_SUBZONE_WEB_PL shapefile into R as a polygon feature data frame. Note that when the input geospatial data is in shapefile format, two arguments will be used, namely: dsn to define the data path and layer to provide the shapefile name. Also note that no extension such as .shp, .dbf, .prj and .shx are needed.

mpsz = st_read(dsn = "data/geospatial", 
                  layer = "MP14_SUBZONE_WEB_PL")
Reading layer `MP14_SUBZONE_WEB_PL' from data source 
  `C:\kekekay\ISSS624\Hands-on Ex1\data\geospatial' using driver `ESRI Shapefile'
Simple feature collection with 323 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21

we can see the ‘Geometry Type’ is multipolygon

Importing polyline feature data in shapefile form

below uses st_read() function of sf package to import CyclingPath shapefile into R as line feature data frame.

getwd()
[1] "C:/kekekay/ISSS624/Hands-on Ex1"
cyclingpath = st_read(dsn = "data/geospatial", 
                         layer = "CyclingPathGazette")
Reading layer `CyclingPathGazette' from data source 
  `C:\kekekay\ISSS624\Hands-on Ex1\data\geospatial' using driver `ESRI Shapefile'
Simple feature collection with 2558 features and 2 fields
Geometry type: MULTILINESTRING
Dimension:     XY
Bounding box:  xmin: 11854.32 ymin: 28347.98 xmax: 42626.09 ymax: 48948.15
Projected CRS: SVY21

Importing GIS data in kml format

The code chunk below will be used to import the kml into R. 

preschool = st_read("data/geospatial/PreSchoolsLocation.kml")
Reading layer `PRESCHOOLS_LOCATION' from data source 
  `C:\kekekay\ISSS624\Hands-on Ex1\data\geospatial\PreSchoolsLocation.kml' 
  using driver `KML'
Simple feature collection with 2290 features and 2 fields
Geometry type: POINT
Dimension:     XYZ
Bounding box:  xmin: 103.6878 ymin: 1.247759 xmax: 103.9897 ymax: 1.462134
z_range:       zmin: 0 zmax: 0
Geodetic CRS:  WGS 84

The message above reveals that preschool is a point feature data frame. There are a total of 1359 features and 2 fields. Different from the previous two simple feature data frame, preschool is in wgs84 coordinates system.

to be continued - 1.5

Checking the Content of A Simple Feature Data Frame

st_geometry(mpsz)
Geometry set for 323 features 
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21
First 5 geometries:
MULTIPOLYGON (((31495.56 30140.01, 31980.96 296...
MULTIPOLYGON (((29092.28 30021.89, 29119.64 300...
MULTIPOLYGON (((29932.33 29879.12, 29947.32 298...
MULTIPOLYGON (((27131.28 30059.73, 27088.33 297...
MULTIPOLYGON (((26451.03 30396.46, 26440.47 303...